package auctionhaus

import org.springframework.dao.DataIntegrityViolationException

class BidController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [bidInstanceList: Bid.list(params), bidInstanceTotal: Bid.count()]
    }

    def create() {
        def crit = Listing.createCriteria()
        def listings = crit.list() {
            gt("endTime", new Date())
        }

        def customers = Customer.list()

        [bidInstance: new Bid(params), listings: listings, customers: customers]
    }

    def save() {
        //println "saving bid"
        def bidInstance = new Bid(params)
        //if (!bidInstance.save(flush: true)) {
        println params.get("listing.id")
        println bidInstance.listing.id
        def listingInstance = Listing.findById(params.get("listing.id"))
        listingInstance.bids.add(bidInstance)
        if (!bidInstance.save(flush: true)) {
        //if (!listingInstance.save(flush: true)) {
            //println "save failed"
            //render(view: "create", model: [bidInstance: bidInstance])
            flash.message = "Your bid was invalid for the following reasons: " + listingInstance.errors
            redirect(controller: "listing", action: "show", id: params.get("listing.id"))
            return
        }

        println "save successful"
		flash.message = message(code: 'default.created.message', args: [message(code: 'bid.label', default: 'Bid'), bidInstance.id])
        if (params.get("listing.id")) {
            redirect(controller: "listing", action: "show", id: params.get("listing.id"))
        }
        else {
            redirect(action: "show", id: bidInstance.id)
        }
    }

    def show() {
        def bidInstance = Bid.get(params.id)
        if (!bidInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'bid.label', default: 'Bid'), params.id])
            redirect(action: "list")
            return
        }

        [bidInstance: bidInstance]
    }

    def edit() {
        def bidInstance = Bid.get(params.id)
        def crit = Listing.createCriteria()
        def listings = crit.list() {
            gt("endTime", new Date())
        }

        def customers = Customer.list()
        if (!bidInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bid.label', default: 'Bid'), params.id])
            redirect(action: "list")
            return
        }

        [bidInstance: bidInstance, listings: listings, customers: customers]
    }

    def update() {
        def bidInstance = Bid.get(params.id)
        if (!bidInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bid.label', default: 'Bid'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (bidInstance.version > version) {
                bidInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'bid.label', default: 'Bid')] as Object[],
                          "Another user has updated this Bid while you were editing")
                render(view: "edit", model: [bidInstance: bidInstance])
                return
            }
        }

        bidInstance.properties = params

        if (!bidInstance.save(flush: true)) {
            render(view: "edit", model: [bidInstance: bidInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'bid.label', default: 'Bid'), bidInstance.id])
        redirect(action: "show", id: bidInstance.id)
    }

    def delete() {
        def bidInstance = Bid.get(params.id)
        if (!bidInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'bid.label', default: 'Bid'), params.id])
            redirect(action: "list")
            return
        }

        try {
            bidInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'bid.label', default: 'Bid'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'bid.label', default: 'Bid'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
